package com.zhangzhan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.constants.SystemConstants;
import com.zhangzhan.domain.commcon.ResponseResult;
import com.zhangzhan.domain.dto.LinkDto;
import com.zhangzhan.domain.dto.LinkListDto;
import com.zhangzhan.domain.dto.SaveLinkDto;
import com.zhangzhan.domain.entity.Link;
import com.zhangzhan.domain.vo.LinkAllVO;
import com.zhangzhan.domain.vo.LinkVO;
import com.zhangzhan.domain.vo.PageLinkVO;
import com.zhangzhan.domain.vo.PageVO;
import com.zhangzhan.mapper.LinkMapper;
import com.zhangzhan.service.LinkService;
import com.zhangzhan.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2022-09-11 21:00:28
 */
@Service

public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    //友链查询
    @Override
    public ResponseResult getAllLink() {
        //只展示审核通过的
        LambdaQueryWrapper<Link> linkWrapper = new LambdaQueryWrapper<>();
        linkWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_STRING);


        List<Link> linkList = this.list(linkWrapper);

        //封装成vo返回
        List<LinkAllVO> vos = BeanCopyUtils.beanCopyUtil(linkList, LinkAllVO.class);

        return ResponseResult.okResult(vos);
    }

    /**
     * @description 分页查询
     * @param pageNum
     * @param pageSize
     * @param dto
     * @return com.zhangzhan.domain.commcon.ResponseResult<com.zhangzhan.domain.vo.PageVO>
     * @author 31115
     * @date 2022/12/8 17:16
     */
    @Override
    public ResponseResult<PageVO> listLink(Integer pageNum, Integer pageSize, LinkListDto dto) {
        Page<Link> linkPage = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(dto.getName()),Link::getName,dto.getName())
                .eq(StringUtils.hasText(dto.getStatus()),Link::getStatus,dto.getStatus());


        page(linkPage,wrapper);
        List<PageLinkVO> vo = BeanCopyUtils.beanCopyUtil(linkPage.getRecords(), PageLinkVO.class);


        return ResponseResult.okResult(new PageVO(vo,linkPage.getTotal()));
    }

    /**
     * @description 新增
     * @param dto
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/8 17:28
     */
    @Override
    public ResponseResult saveLink(SaveLinkDto dto) {
        if (!StringUtils.hasText(dto.getName())){
            return ResponseResult.errorResult(500, "友联名称不能为空");
        }
        if (!StringUtils.hasText(dto.getAddress())){
            return ResponseResult.errorResult(500, "友联地址不能为空");
        }

        Link link = BeanCopyUtils.beanCopyUtils(dto, Link.class);
        save(link);

        return ResponseResult.okResult();
    }

    /**
     * @description 修改前查询
     * @param id
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/8 17:36
     */
    @Override
    public ResponseResult selecyById(Long id) {
        if (id >= 0) {
            Link link = getById(id);
            LinkVO vo = BeanCopyUtils.beanCopyUtils(link, LinkVO.class);
            return ResponseResult.okResult(vo);
        } else {
            return ResponseResult.errorResult(500, "id不合法");
        }
    }

    /**
     * @description 修改
     * @param dto
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/8 17:45
     */
    @Override
    public ResponseResult updateLink(LinkDto dto) {
        if (!StringUtils.hasText(dto.getName())){
            return ResponseResult.errorResult(500, "友联名称不能为空");
        }
        if (!StringUtils.hasText(dto.getAddress())){
            return ResponseResult.errorResult(500, "友联地址不能为空");
        }

        Link link = BeanCopyUtils.beanCopyUtils(dto, Link.class);
        updateById(link);

        return ResponseResult.okResult();
    }

    /**
     * @description 删除
     * @param ids
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/8 17:48
     */
    @Override
    public ResponseResult deleteLinkByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        Link link = new Link();
        for (Long id : idList) {
            if (id >= 0) {
                LambdaUpdateWrapper<Link> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(Link::getId,id)
                        .set(Link::getDelFlag,"1");
                update(link,wrapper);
            } else {
                return ResponseResult.errorResult(500, "id不合法");
            }
        }
        return ResponseResult.okResult();
    }
}

